/*
 * GUIMainInterface.java
 *
 * Created on Apr 15, 2011, 4:39:37 PM
 */
package horsechess.client.gui;

import horsechess.client.data.PlayerData;
import horsechess.client.handle.ChessBoard;
import horsechess.client.handle.Dice;
import horsechess.client.handle.PlayerConnect;
import horsechess.packet.PlayerPacket;
import java.awt.BorderLayout;
import java.awt.Image;
import java.util.List;
import javax.swing.ImageIcon;

/**
 *
 * @author Administrator
 */
public class GUIClient extends javax.swing.JFrame {

    /** Creates new form GUIMainInterface */
    public GUIClient() {
        player = new PlayerConnect(this);
        initComponents();
        customInitComponents();
//        configGame();
    }

    /**
     * This method used for init component manually.
     */
    private void customInitComponents() {
        btnRollingDice.setText("Rolling the dice");
        mniOption.setText("Option");
        mniConfig.setText("Config");
        mniChat.setText("Chat");
        mniExit.setText("Exit");
        mniHelp.setText("Help");
        this.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/resource/Other/icon.png")).getImage());
        pnlChessBoard = new GUIChessBoard();
        pnlChessBoard.setSize(640, 646);
        this.add(pnlChessBoard, BorderLayout.CENTER);
        customizeArgu = new GUIOption(this, "The configuration dialog", player);
        jLabel1.setText("Turn of: ");
        guiDice.setVisible(false);
        lblScoreTitle.setText("Score: ");
        lblScore.setText("88");
        jLabel2.setText("Player name:");
        //JScrollPane scrollPane = new JScrollPane(txtMessage);
        btnEndTurn.setText("End Of Your Turn");
    }

    private void setUpTimer() {

        //clockLabel.setText("")
        clockLabel.setVisible(false);
        clockLabel.setText("Time:" + time + "");
    }

    /**
     * Load image of the chessboard
     */
    public void loadImageChessBoard() {
        pnlChessBoard.repaint();
    }

    /**
     * get String url of image
     * @return
     */
    public static String getStrImage() {
        String s = "/resource/horse/";
        switch (cb.getInformationBoard().getCurrentPlayer()) {
            case 1:
                s += "GREEN.gif";
                break;
            case 2:
                s += "RED.gif";
                break;
            case 3:
                s += "YELLOW.gif";
                break;
            case 4:
                s += "BLUE.gif";
                break;
        }
        return s;
    }

    public static String getStrImage(int n) {
        String s = "/resource/horse/";
        switch (n) {
            case 1:
                s += "GREEN.gif";
                break;
            case 2:
                s += "RED.gif";
                break;
            case 3:
                s += "YELLOW.gif";
                break;
            case 4:
                s += "BLUE.gif";
                break;
        }
        return s;
    }

    /**
     * Reset the interface
     */
    public void resetInterface() {
        loadImageChessBoard();
        //System.out.println("Number of dice: "+customizeArgu.cData.getNumberOfDice());
        guiDice.setNumberOfDice(customizeArgu.cData.getNumberOfDice());
        guiDice.localizeDice();
        cb.getInformationBoard().updateData(customizeArgu.cData);
    }

    public boolean chkStartBattleValue() {
        lblMessage.setText("");
        if (dice.getNumberDices() == 1) {
            if (dice.getDiceValue1() == 1
                    || dice.getDiceValue1() == 6) {
                return true;
            }
        } else {
            // 1 +1,2+2,3+3,4+4,5+5 hoac 6 + 6 dc ra quan
            if ((dice.getDiceValue1() == 1 && dice.getDiceValue2() == 1)
                    || (dice.getDiceValue1() == 2 && dice.getDiceValue2() == 2)
                    || (dice.getDiceValue1() == 3 && dice.getDiceValue2() == 3)
                    || (dice.getDiceValue1() == 4 && dice.getDiceValue2() == 4)
                    || (dice.getDiceValue1() == 5 && dice.getDiceValue2() == 5)
                    || (dice.getDiceValue1() == 6 && dice.getDiceValue2() == 6)) {
                return true;
            }

            if ((dice.getDiceValue1() == 6) && (dice.getDiceValue2() == 1)
                    || (dice.getDiceValue1() == 1) && (dice.getDiceValue2() == 6)) {
                return true;
            }
        }
        return false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        pnlController = new javax.swing.JPanel();
        btnRollingDice = new javax.swing.JButton();
        pnlPlayerInfo = new javax.swing.JPanel();
        lblUser = new javax.swing.JLabel();
        picTurn = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lblScore = new javax.swing.JLabel();
        lblScoreTitle = new javax.swing.JLabel();
        lblEndingMessage = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lblPlayerName = new javax.swing.JLabel();
        guiDice = new horsechess.client.gui.GUIDice();
        clockLabel = new javax.swing.JLabel();
        lblScoreOfPlayer = new javax.swing.JLabel();
        btnEndTurn = new javax.swing.JButton();
        lblTimeOfMatch = new javax.swing.JLabel();
        pnlStatusBar = new javax.swing.JPanel();
        lblMessage = new javax.swing.JLabel();
        mnBar = new javax.swing.JMenuBar();
        mniOption = new javax.swing.JMenu();
        mniConfig = new javax.swing.JMenuItem();
        mniChat = new javax.swing.JMenuItem();
        mniExit = new javax.swing.JMenuItem();
        mniHelp = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("The Horse racing ");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setFocusable(false);
        setFocusableWindowState(false);
        setForeground(java.awt.Color.black);
        setMinimumSize(new java.awt.Dimension(1010, 730));
        setName("frmMainGUI"); // NOI18N
        setResizable(false);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${background}"), this, org.jdesktop.beansbinding.BeanProperty.create("background"));
        bindingGroup.addBinding(binding);

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        pnlController.setBackground(new java.awt.Color(255, 255, 255));
        pnlController.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Operation controller", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Comic Sans MS", 1, 14), new java.awt.Color(0, 0, 204))); // NOI18N
        pnlController.setPreferredSize(new java.awt.Dimension(360, 200));
        pnlController.setLayout(null);

        btnRollingDice.setBackground(new java.awt.Color(0, 102, 153));
        btnRollingDice.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        btnRollingDice.setForeground(new java.awt.Color(204, 255, 204));
        btnRollingDice.setAutoscrolls(true);
        btnRollingDice.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED, java.awt.Color.orange, new java.awt.Color(255, 51, 51), java.awt.Color.gray, java.awt.Color.black));
        btnRollingDice.setBorderPainted(false);
        btnRollingDice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRollingDiceActionPerformed(evt);
            }
        });
        pnlController.add(btnRollingDice);
        btnRollingDice.setBounds(10, 350, 340, 40);

        pnlPlayerInfo.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayerInfo.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Info of player", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12), new java.awt.Color(255, 51, 102))); // NOI18N
        pnlPlayerInfo.setLayout(null);

        lblUser.setFont(new java.awt.Font("Arial", 1, 12));
        pnlPlayerInfo.add(lblUser);
        lblUser.setBounds(90, 30, 200, 30);

        picTurn.setMaximumSize(new java.awt.Dimension(40, 40));
        picTurn.setMinimumSize(new java.awt.Dimension(40, 40));
        picTurn.setPreferredSize(new java.awt.Dimension(40, 40));
        pnlPlayerInfo.add(picTurn);
        picTurn.setBounds(150, 110, 40, 40);

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        jLabel1.setForeground(new java.awt.Color(0, 0, 204));
        pnlPlayerInfo.add(jLabel1);
        jLabel1.setBounds(20, 30, 60, 20);

        lblScore.setFont(new java.awt.Font("Tahoma", 1, 12));
        pnlPlayerInfo.add(lblScore);
        lblScore.setBounds(100, 70, 50, 20);

        lblScoreTitle.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        pnlPlayerInfo.add(lblScoreTitle);
        lblScoreTitle.setBounds(20, 70, 60, 18);

        pnlController.add(pnlPlayerInfo);
        pnlPlayerInfo.setBounds(10, 120, 340, 160);

        lblEndingMessage.setFont(new java.awt.Font("Comic Sans MS", 1, 18)); // NOI18N
        lblEndingMessage.setForeground(new java.awt.Color(0, 102, 255));
        pnlController.add(lblEndingMessage);
        lblEndingMessage.setBounds(10, 450, 340, 30);

        jLabel2.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabel2.setForeground(new java.awt.Color(102, 51, 0));
        pnlController.add(jLabel2);
        jLabel2.setBounds(30, 90, 140, 20);

        lblPlayerName.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblPlayerName.setForeground(new java.awt.Color(51, 0, 51));
        pnlController.add(lblPlayerName);
        lblPlayerName.setBounds(180, 90, 120, 20);
        pnlController.add(guiDice);
        guiDice.setBounds(230, 290, 104, 50);

        clockLabel.setFont(new java.awt.Font("Comic Sans MS", 1, 18));
        clockLabel.setForeground(new java.awt.Color(51, 0, 51));
        clockLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/Other/clock.gif"))); // NOI18N
        pnlController.add(clockLabel);
        clockLabel.setBounds(10, 290, 210, 50);

        lblScoreOfPlayer.setFont(new java.awt.Font("Verdana", 1, 12));
        lblScoreOfPlayer.setForeground(new java.awt.Color(0, 102, 102));
        pnlController.add(lblScoreOfPlayer);
        lblScoreOfPlayer.setBounds(310, 30, 40, 20);

        btnEndTurn.setBackground(new java.awt.Color(204, 204, 0));
        btnEndTurn.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        btnEndTurn.setForeground(new java.awt.Color(51, 0, 51));
        btnEndTurn.setAutoscrolls(true);
        btnEndTurn.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED, java.awt.Color.orange, new java.awt.Color(255, 51, 51), java.awt.Color.gray, java.awt.Color.black));
        btnEndTurn.setBorderPainted(false);
        btnEndTurn.setEnabled(false);
        btnEndTurn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEndTurnActionPerformed(evt);
            }
        });
        pnlController.add(btnEndTurn);
        btnEndTurn.setBounds(10, 400, 340, 40);

        lblTimeOfMatch.setFont(new java.awt.Font("Comic Sans MS", 1, 18));
        lblTimeOfMatch.setForeground(new java.awt.Color(153, 0, 51));
        pnlController.add(lblTimeOfMatch);
        lblTimeOfMatch.setBounds(60, 30, 240, 40);

        getContentPane().add(pnlController, java.awt.BorderLayout.EAST);

        pnlStatusBar.setBackground(new java.awt.Color(255, 255, 255));
        pnlStatusBar.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        pnlStatusBar.setPreferredSize(new java.awt.Dimension(100, 20));
        pnlStatusBar.setLayout(null);

        lblMessage.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        lblMessage.setForeground(new java.awt.Color(153, 51, 0));
        pnlStatusBar.add(lblMessage);
        lblMessage.setBounds(0, 0, 610, 20);

        getContentPane().add(pnlStatusBar, java.awt.BorderLayout.PAGE_END);

        mnBar.setBackground(new java.awt.Color(255, 255, 255));

        mniConfig.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/Other/config.gif"))); // NOI18N
        mniConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniConfigActionPerformed(evt);
            }
        });
        mniOption.add(mniConfig);

        mniChat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/Other/chat.png"))); // NOI18N
        mniChat.setEnabled(false);
        mniChat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniChatActionPerformed(evt);
            }
        });
        mniOption.add(mniChat);

        mniExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/Other/exit.png"))); // NOI18N
        mniExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniExitActionPerformed(evt);
            }
        });
        mniOption.add(mniExit);

        mnBar.add(mniOption);

        mniHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniHelpActionPerformed(evt);
            }
        });
        mnBar.add(mniHelp);

        setJMenuBar(mnBar);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        loadImageChessBoard();
        this.dice.setNumberDices(customizeArgu.cData.getNumberOfDice());
        this.btnRollingDice.setEnabled(false);
    }//GEN-LAST:event_formWindowOpened

    private void btnRollingDiceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRollingDiceActionPerformed

        //User roll dice
        guiDice.setVisible(true);
        player.rollDice();
        btnEndTurn.setEnabled(true);
    }//GEN-LAST:event_btnRollingDiceActionPerformed

    public void processTurn(int dice1, int dice2) {
        dice.setDiceValue1(dice1);
        dice.setDiceValue2(dice2);
        dice.rollDice(guiDice);
        picTurn.setIcon(new javax.swing.ImageIcon(getClass().getResource(getStrImage())));
        String playerName = this.cb.getInformationBoard().players.get(cb.getInformationBoard().getCurrentPlayer() - 1).getUsername();
        lblUser.setText(playerName);
        lblMessage.setText(playerName + " is rolling the dice");
        // Update dice value
        cb.getInformationBoard().updateDiceValue(dice);

        //Join into board
        bEnter = chkStartBattleValue();
        //System.out.println("Check start battle Value: " + bEnter);

        //Neu ngua co the di
        //System.out.println("Ngua co the di ko: " + cb.isMove());
        if (playerName.equals(lblPlayerName.getText().trim())) {
            lblMessage.setText("Click on horse icon to move the horse " + dice.getDiceValue() + " step");
            btnEndTurn.setEnabled(true);
        } else {
            lblMessage.setText("Please wait for other players move their horse");
            btnEndTurn.setEnabled(false);
        }
        btnRollingDice.setEnabled(false);

        //  System.out.println("Ngua co the move ko: " + cb.isMove());
        if (cb.isMove()) {
            cb.processChessBoard();
            player.controlClock(3);
            btnEndTurn.setEnabled(true);

        } else {
            btnEndTurn.setEnabled(false);
            isSameTurn = false;
            turnToNextPlayer();
        }

        if (!bEnter) {
            isSameTurn = false;

        } else {
            player.controlClock(3);
            btnRollingDice.setEnabled(true);
            btnEndTurn.setEnabled(true);
            lblMessage.setText("You got another turn!! Please roll the dice!!");
            isSameTurn = true;
        }
    }

    public void turnToNextPlayer() {
        if (!isSameTurn) {
            //clockLabel.setVisible(false);
            btnRollingDice.setEnabled(false);
            int nextPlayer = cb.getInformationBoard().getCurrentPlayer() % cb.getInformationBoard().getNumberPlayers();
            cb.getInformationBoard().setCurrentPlayer(nextPlayer + 1);
            handleTurnOfPlayer();

        }
    }

    public void updateTurn(int currPlayer) {
        cb.getInformationBoard().setCurrentPlayer(currPlayer);
        handleTurnOfPlayer();
        player.controlClock(3);
    }

    public void handleTurnOfPlayer() {
        PlayerData currPlayer = cb.getInformationBoard().players.get(cb.getInformationBoard().getCurrentPlayer() - 1);
        //  System.out.println("Turn of " + currPlayer.getUsername());
        if (currPlayer.getUsername().equalsIgnoreCase(lblPlayerName.getText())) {
            lblEndingMessage.setText("YOUR TURN");

            btnRollingDice.setEnabled(true);
            lblMessage.setText("Please rolling the dice!!!");
            clockLabel.setVisible(true);
            player.controlClock(3);
//            btnEndTurn.setEnabled(true);
        } else {
            lblEndingMessage.setText("OTHER TURN");
            btnRollingDice.setEnabled(false);
            lblMessage.setText("Please wait for other player to play their game!!!");
            clockLabel.setVisible(false);
        }
        picTurn.setIcon(new ImageIcon(getClass().getResource(getStrImage())));
        lblUser.setText(currPlayer.getUsername());
        lblScore.setText(currPlayer.getScore() + "");
    }

    private void mniChatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniChatActionPerformed
        uIClient.customizeArgu.setVisible(true);
        uIClient.customizeArgu.setLocation(660, 530);
        uIClient.customizeArgu.optionTab.setSelectedIndex(3);
    }//GEN-LAST:event_mniChatActionPerformed

    public void updateTime(String time) {
        if (lblEndingMessage.getText().equalsIgnoreCase("YOUR TURN")) {
            clockLabel.setText(time);
            if (time.equalsIgnoreCase("Timeout!!!")) {
                ignoreTurn++;
                int currPlayer = cb.getInformationBoard().getCurrentPlayer() - 1;
//                if (ignoreTurn == 2 && cb.getInformationBoard().getNumberPlayers() > 2) {
//                    disconnectedPlayer(lblPlayerName.getText().trim());
//                    player.send("@Disconnected#" + lblPlayerName.getText());
//
//                }
                if (ignoreTurn == 2 && cb.getInformationBoard().getNumberPlayers() == 2) {
                    cb.getInformationBoard().players.get(currPlayer).getUsername();
                    lose(cb.getInformationBoard().players.get(currPlayer));

                    System.out.println("@Won#" + cb.getInformationBoard().players.get(getNextPlayer()).getUsername() + "#" + getNextPlayer());
                    player.send("@Won#" + cb.getInformationBoard().players.get(getNextPlayer()).getUsername() + "#" + getNextPlayer());
                    return;
                }
                turnToNextPlayer();
                player.turnTo(cb.getInformationBoard().getCurrentPlayer());

            }

        }
    }

    private int getNextPlayer() {
        int nextPlayer = cb.getInformationBoard().getCurrentPlayer() % cb.getInformationBoard().getNumberPlayers();
        return nextPlayer;
    }

    public void startGame() {
        clockLabel.setText("Ticking clock!!");
        try {

            resetInterface();
            cb.arrangeChessBoard(customizeArgu.cData, lblMessage);
            mniChat.setEnabled(true);
            lblTimeOfMatch.setVisible(true);
//            btnEndTurn.setEnabled(true);
            btnRollingDice.setEnabled(true);
            cb.getInformationBoard().setCurrentPlayer(1);
            handleTurnOfPlayer();
            guiDice.setVisible(false);

//            System.out.println("Image turn: " + getStrImage());
//            lblUser.setText(this.cb.getInformationBoard().players.get(cb.getInformationBoard().getCurrentPlayer() - 1).getUsername());
//            picTurn.setIcon(new javax.swing.ImageIcon(getClass().getResource(getStrImage())));
        } catch (Exception ex) {
            ex.printStackTrace();
            lblMessage.setText("Please configuration before played game");
        }
    }

    private void mniConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniConfigActionPerformed
        customizeArgu.setVisible(true);
    }//GEN-LAST:event_mniConfigActionPerformed

    public void disconnectedPlayer(String playerName) {
        try {
            cb.removePlayer(playerName);
        } catch (Exception e) {
            lblMessage.setText("Fail to disconnect with game: " + e.getMessage());
        }
        lblMessage.setText(playerName + " has been disconnected with game !!!");
    }

    public void updateScore() {
        List<PlayerData> players = cb.getInformationBoard().players;
        for (int i = 0; i < players.size(); i++) {
            PlayerData playerData = players.get(i);
            if (lblPlayerName.getText().trim().equals(playerData.getUsername())) {
                System.out.println("Score of player: " + playerData.getScore());
                lblScoreOfPlayer.setText(playerData.getScore() + "");
            }
            if (lblUser.getText().trim().equals(playerData.getUsername())) {
                lblScore.setText(playerData.getScore() + "");
            }
            if (playerData.getScore() > 20) {
                player.send("@Won#" + playerData.getUsername() + "#" + i);
                System.out.println("Send win: " + "@Won#" + playerData.getUsername() + "#" + i);
                for (int j = 0; j < players.size(); j++) {
                    PlayerData temp = players.get(j);
                    if (!temp.equals(playerData)) {
                        player.send("@Lose#" + temp.getUsername() + "#" + j);
                        System.out.println("Send Lose: " + "@Lose#" + temp.getUsername() + "#" + j);
                    }
                }
            }
        }
    }

    public void updateMoving(String playerName, int horse) {
        List<PlayerData> players = cb.getInformationBoard().players;
        for (int i = 0; i < players.size(); i++) {
            try {
                PlayerData player = players.get(i);
                if (player.getUsername().equalsIgnoreCase(playerName.trim())) {
                    player.listOfChessMan.get(horse).getGUIChessMan().processMoving();
                }
            } catch (Exception e) {
                endTurn();
            }
        }
    }

    /**
     * Game Over
     * @param evt
     */
    public void gameOver() {
        lblEndingMessage.setVisible(true);
        lblEndingMessage.setText("Game Over. !!! Make the configuration and play again ^^");
        lblMessage.setText("You have been game over this game!!!!");
        btnRollingDice.setVisible(false);
        btnEndTurn.setEnabled(false);
        clockLabel.setVisible(false);
    }

    /**
     * Losing
     * @param evt
     */
    public void win(PlayerData winner, boolean wonFromServer) {
        btnRollingDice.setEnabled(false);
        btnEndTurn.setEnabled(false);
        lblEndingMessage.setVisible(true);
        lblEndingMessage.setText("WAOOOO!!! " + winner.getUsername() + " has been winning the game!!!!");
        lblMessage.setText("Biz Biz Biz Biz !! ^-^ :D Bravo you got the score: " + winner.getScore() + "!!!!!!!!");
        if (!wonFromServer) {
            for (int i = 0; i < cb.getInformationBoard().players.size(); i++) {
                PlayerData playerData = cb.getInformationBoard().players.get(i);
                player.send("@Lose#" + playerData.getUsername() + "#" + i);
            }
        }
    }

    public void lose(PlayerData loser) {
        lblEndingMessage.setVisible(true);
        lblEndingMessage.setText("Opps!!! " + loser.getUsername() + " has been lost the game!!!!");
        lblMessage.setText("Unlucky for you!!!! You got the score: " + loser.getScore() + "!!!!!!!!");
        btnRollingDice.setEnabled(false);
    }

    private void mniExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniExitActionPerformed
        dispose();
    }//GEN-LAST:event_mniExitActionPerformed

    private void btnEndTurnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEndTurnActionPerformed
        endTurn();
    }//GEN-LAST:event_btnEndTurnActionPerformed

    private void mniHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniHelpActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_mniHelpActionPerformed

    private void endTurn() {
        isSameTurn = false;
        turnToNextPlayer();
        player.controlClock(2);
        btnEndTurn.setEnabled(false);
        player.turnTo(cb.getInformationBoard().getCurrentPlayer());
    }

    public void calculateHighgestPlayer() {
        if (cb.getInformationBoard().getPlayerHighestScore() != null) {
            PlayerData pd = cb.getInformationBoard().getPlayerHighestScore();
            player.send("@Won#" + pd.getUsername() + "#" + cb.getInformationBoard().players.indexOf(pd));
            for (int i = 0; i < cb.getInformationBoard().players.size(); i++) {
                PlayerData playerData = cb.getInformationBoard().players.get(i);
                if(!playerData.equals(pd))
                player.send("@Lose#" + playerData.getUsername() + "#" + i);
            }
        } else {
            System.out.println("Send game over");
            player.send("@GAME_OVER");
        }
    }

    /**
     * Timer action for the player
     * @param e
     */
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                uIClient = new GUIClient();
                uIClient.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                uIClient.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton btnEndTurn;
    public javax.swing.JButton btnRollingDice;
    public javax.swing.JLabel clockLabel;
    public static horsechess.client.gui.GUIDice guiDice;
    private javax.swing.JLabel jLabel1;
    javax.swing.JLabel jLabel2;
    public static javax.swing.JLabel lblEndingMessage;
    public static javax.swing.JLabel lblMessage;
    public static javax.swing.JLabel lblPlayerName;
    public javax.swing.JLabel lblScore;
    public javax.swing.JLabel lblScoreOfPlayer;
    private javax.swing.JLabel lblScoreTitle;
    public static javax.swing.JLabel lblTimeOfMatch;
    public static javax.swing.JLabel lblUser;
    private javax.swing.JMenuBar mnBar;
    public javax.swing.JMenuItem mniChat;
    private javax.swing.JMenuItem mniConfig;
    private javax.swing.JMenuItem mniExit;
    private javax.swing.JMenu mniHelp;
    private javax.swing.JMenu mniOption;
    public static javax.swing.JLabel picTurn;
    private javax.swing.JPanel pnlController;
    private javax.swing.JPanel pnlPlayerInfo;
    private javax.swing.JPanel pnlStatusBar;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    /// Dice
    private static Dice dice = new Dice();
    ///Customize Argument
    public static GUIOption customizeArgu;
    private Boolean bEnter = false;
    public static ChessBoard cb = new ChessBoard();
    private Image picChessboard;
    public GUIChessBoard pnlChessBoard;
    public PlayerConnect player;
    public static GUIClient uIClient;
    private static boolean isSameTurn = false;
    private static boolean startGame = true;
    private int time = 30;
    private int ignoreTurn = 0;
}
